<?php
/**
 * 文件处理器抽象类
 *
 * @version $Id$
 * @package if
 * @subpackage util.io
 */
abstract class IAbstractFileProcessor extends IObject {
	private $locking = false;
	private $isLocked = true;
	
	/**
	 * Enter description here...
	 *
	 * @var IFileHandler
	 */
	private $handler;
	
	/**
	 * 锁定
	 *
	 */
	function lock() {
		$this->locking = true;
	}
	
	/**
	 * 释放锁定
	 *
	 */
	function release() {
		$this->locking = false;
		$this->isLocked = false;
		if ($this->handler) {
			$this->handler->release();
		}
	}
	
	/**
	 * 关闭当前处理器
	 *
	 */
	function close() {
		if ($this->handler) {
			$this->release();
			$this->handler->close();
			$this->handler = null;
		}
	}
	
	/**
	 * 取得文件句柄
	 *
	 * @param IFile $file 文件对象
	 * @param string $mode 文件打开模式
	 * @param string $lockType 锁定类型
	 * @return IFileHandler
	 */
	protected function fileHandler(IFile $file, $mode, $lockType) {
		if (!$this->handler) {
			$this->handler = new IFileHandler($file, $mode);
			if ($this->handler->isReady()) {
				if ($this->locking == true && !$this->isLocked) {
					$this->handler->lock($lockType);
					$this->isLocked = true;
				}
			}
		}
		return $this->handler;
	}
	
	/**
	 * 取得文件句柄
	 *
	 * @return resource
	 */
	public abstract function handler();
}


?>